package com.zz.ecommerce.declareManage.exportB2CDeclare.orderManage;

import com.alibaba.fastjson.JSONObject;
import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.pojo.CbecOrder;
import com.zz.ecommerce.pojo.CbecOrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by steven on 2016/3/28.
 */
@Controller
@RequestMapping("/exportB2C/orderManage")
public class Eb2cOrderController {

    @Autowired
    private Eb2cOrderDao orderDao;

    @Autowired
    private Eb2cOrderItemDao orderItemDao;

    @RequestMapping("/init")
    public String init() {
        return "declareManage/exportB2CDeclare/orderManage/index";
    }

    @RequestMapping("/queryOrderList")
    @ResponseBody
    public GridResult queryOrderList(CbecOrder orderHead, GridRequest request) {
        int count = orderDao.count(TableConstants.CBEC_ORDER, orderHead);
        List<CbecOrder> orderHeadList = orderDao.page(TableConstants.CBEC_ORDER, orderHead,
                request.getStart(), request.getEnd(), request.getSortOrder());

        GridResult result = new GridResult(orderHeadList, request, count);
        return result;
    }

    @RequestMapping("/showDetail")
    public String showDetail(@RequestParam(value = "id") String id, GridRequest request, ModelMap modelMap) {
        CbecOrder query = new CbecOrder();
        query.setId(id);
        CbecOrder orderHead = orderDao.query(TableConstants.CBEC_ORDER, query);

        CbecOrderItem orderItem = new CbecOrderItem();
        orderItem.setOrderId(id);
        request.setSidx("gNum");
        request.setSord("asc");
        List<CbecOrderItem> orderItemList = orderItemDao.queryAll(TableConstants.CBEC_ORDER_ITEM, orderItem,
                request.getSortOrder());

        modelMap.put("orderHead", orderHead);
        modelMap.put("orderBodyListJSON", JSONObject.toJSONString(orderItemList));

        return "declareManage/exportB2CDeclare/orderManage/showDetail";
    }
}
